<template>
  <div class="sm-cs-changeSort">
    <TableTitle>
      <slot>
        <p>
          扫码操作
        </p>
      </slot>
    </TableTitle>
    <TopSearch ref="topSm">
      <slot>
        <li>
          <el-button size="mini" type="primary" @click="gzbComeBtn">工作包出库</el-button>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="gzbScan">工作包扫码</el-button>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="gzbBackBtn">工作包回库</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="hcxComeBtn">缓存箱出库</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="hcxScan">缓存箱扫码</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="hcxBoxBackBtn">缓存箱回库</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="saveBtn">分拣保存</el-button>
        </li>
        <li>
          <p>
            工作包条码：<span>{{gzbTM}}</span>
          </p>
          <p>
            缓存箱条码：<span>{{hcxTM}}</span>
          </p>
        </li>
      </slot>
    </TopSearch>
    <TopData></TopData>
    <TableTitle>
      <slot>
        <p>
          AO分拣任务列表
        </p>
      </slot>
    </TableTitle>
    <TopTable></TopTable>
    <TableTitle>
      <slot>
        <p>
          AO物料配套清单
        </p>
      </slot>
    </TableTitle>
    <MidTable></MidTable>
    <LastGoods></LastGoods>
  </div>
</template>
<script>
import TopSearch from "../../../../components/TopSearch.vue";
import TopTable from "./components/TopTable.vue"
import MidTable from "./components/MidTable.vue"
import TopData from "./components/TopData.vue"
import TableTitle from "../../../../components/TableTitle.vue"
import LastGoods from "./components/LastGoods.vue"
import { smSmanHcxScan,smSmanGzbScan,workReSave,smSmanCsHcxOut,smSmanCsHcxBack,workBoxSmBack,workBoxSmOut } from "@/utils/api.js"
export default {
  name: "changeSort",
  components: {
    TopSearch,
    TopTable,
    MidTable,
    TopData,
    TableTitle,
    LastGoods
  },
  data(){
    return{
      hcxGjbInfo:[],
      // 缓存箱出库按钮信息list
      hcxList:[],
      indexTm:'',
      hcxTM:'',
      gzbTM:'',
      gzbListInfo:'',
      hcxBoxBack:'',
      hcxBackTm:'',
      hcxBoxCome:[],
      //yebo 新增属性
      packageInfo:[], 
      matchInfo:[]
    }
  },
  methods:{
    // 统一扫码判断接口方法
    scanning(){
      if(this.indexTm.length == 12){
        let indexTmList = [...this.indexTm]
        if(indexTmList[0] == 1){
          // 缓存箱扫码
          this.$options.methods.hcxScan.bind(this)()
        }else if(indexTmList[0] == 2){
          // 工作包扫码
          this.$options.methods.gzbScan.bind(this)()
        }
      }else if(this.indexTm.length == 5){
        // 缓存箱出库 工作包出库
       // this.$options.methods.hcxComeBtn.bind(this)(this.indexTm)
       // this.$options.methods.gzbComeBtn.bind(this)(this.indexTm)
      }else if(this.indexTm == 9002){
        // 缓存箱回库
        this.$options.methods.hcxBoxBackBtn.bind(this)()
      }else if(this.indexTm == 9004){
        // 工作包回库
        this.$options.methods.gzbBackBtn.bind(this)()
      }else if(this.indexTm == 9005){
        // 分拣保存
        this.$options.methods.saveBtn.bind(this)()
      }else{
        // this.$bus.$emit('mrMwMaterialListSend',this.indexTm)
        // 物料确认
        if(this.indexTm.includes('{')){
          for(let mn=0;mn<this.marterialResult.length;mn++){
            if(this.indexTm.CPHGZ == this.marterialResult[mn].mesCheckQualiticationId){
              this.$bus.$emit('smCsMaterialNumber',mn)
            }
          }
        }else{
          for(let mn=0;mn<this.marterialResult.length;mn++){
            if(this.indexTm == this.marterialResult[mn].barcode){
              this.$bus.$emit('smCsMaterialNumber',mn)
            }
          }
        }
        
      }
    },
    // 工作包出库  
    gzbComeBtn(tiaoma){
      if(this.packageInfo.length!=1){
        this.$Alert({title: "失败",message: 'AO任务列表请选择一条记录！',type: "error"});
        return;
      }
      var packid = this.packageInfo[0].msTaskPackageId;
      workBoxSmOut({
        agv:'',
        // msTaskAoIds:this.gzbCome
        msTaskPackageId:packid,
        aoBarcode:this.indexTm
      }).then((res)=>{
        console.log('工作包出库',res)
        if(res.data.retCode == 200){
          this.aoNumber = tiaoma
          this.$Alert({
            title: "成功",
            message: '工作包出库命令已下发！',
            type: "success"
          });
          // 出库成功聚焦条码框 并清空
          this.$refs.topSm.makeTmNull()
          this.$refs.topSm.openScan()
        }else {
          this.$Alert({
            title: "失败",
            message: '无法下发工作包出库命令！',
            type: "error"
          });
        }
      })
    },
    // 工作包扫码
    gzbScan(){
      smSmanGzbScan({
        toolCode:this.indexTm,
      }).then((res)=>{
        console.log('工作包扫码',res)
        if(res.data.retCode == 200) {
          this.$bus.$emit('smCsSendGzbInfo',res.data.responseBody)
          this.$Alert({
            title: "成功",
            message: res.data.message,
            type: "success",
          });
          // 拿到数据传给底部框
          document.querySelector('.sm-sman-leftInfo').style.display = 'inline'
          this.gzbTM = this.indexTm
          // 缓存箱扫码成功清空条码框并聚焦
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
        }else {
          this.$Alert({
            title: "失败",
            message: res.data.message,
            type: "error",
          });
        }
      })
    },
    // 工作包回库
    gzbBackBtn(){
      this.gzbBackTm = this.gzbTM
      workBoxSmBack({
          palletCode:this.gzbBackTm
        }).then((res)=>{
          console.log('工作包回库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '工作包回库命令已下发！',
              type: "success",
            });
            document.querySelector('.sm-sman-leftInfo').style.display = 'none'
            this.gzbTM = ''
            // 回库成功清空条码框并聚焦
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发工作包回库命令！',
              type: "error",
            });
          }
        })
    },
    // 缓存箱出库 -----更换对的接口就行 格式已经写好
    hcxComeBtn(tiaoma){
      if(this.packageInfo.length!=1){
        this.$Alert({title: "失败",message: 'AO任务列表请选择一条记录！',type: "error"});
        return;
      }
      var packid = this.packageInfo[0].msTaskPackageId;
      smSmanCsHcxOut({
          // msTaskAoIds:this.hcxBoxCome
          msTaskPackageId:packid,
          aoBarcode:this.indexTm
        }).then((res)=>{
          console.log('缓存箱出库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '缓存箱出库命令已下发！',
              type: "success",
            });
            this.aoNumber = tiaoma
            // 出库成功聚焦条码框 并清空
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发缓存箱出库命令！',
              type: "error",
            });
          }
        })
    },
    // 缓存箱扫码
    hcxScan(){
      smSmanHcxScan({
        workbinCode:this.indexTm
      }).then((res)=>{
        console.log('缓存箱扫码',res)
        if(res.data.retCode == 200) {
          this.hcxTM = this.indexTm
          this.$Alert({
              title: "成功",
              message: res.data.message,
              type: "success",
            });
          // 拿到数据传给底部框
          this.marterialResult = res.data.responseBody
          this.$bus.$emit('smCsSendHcxInfos',res.data.responseBody)
          document.querySelector('.sm-sman-rightInfo').style.display = 'inline'
          // 缓存箱扫码成功清空条码框并聚焦
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
        }else {
          this.$Alert({
              title: "失败",
              message: res.data.message,
              type: "error",
            });
        }
      })
    },
    // 缓存箱回库
    hcxBoxBackBtn(){
      this.hcxBackTm = this.hcxTM
      smSmanCsHcxBack({
          workbinCode:this.hcxBackTm
        }).then((res)=>{
          console.log('缓存箱回库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '缓存箱回库命令已下发！',
              type: "success",
            });
            document.querySelector('.sm-sman-rightInfo').style.display = 'none'
            this.hcxTM = ''
            // 回库成功清空条码框并聚焦
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发缓存箱回库命令！',
              type: "error",
            });
          }
        })
    },
    // 分拣保存
    saveBtn(){
      console.log("分拣保存",this.matchInfo);
      return ;
      workReSave({
        toolCode:this.gzbTM,
        materialDTOs:this.gzbListInfo
      }).then((res)=>{
        console.log('保存',res)
        if(res.data.retCode == 200) {
          this.$Alert({
            title: "成功",
            message: '保存成功',
            type: "success",
          });
          document.querySelector('.sm-sman-rightInfo').style.display = 'none'
          document.querySelector('.sm-sman-leftInfo').style.display = 'none'
        }else {
          this.$Alert({
            title: "失败",
            message: '保存失败',
            type: "error",
          });
        }
      })
    }
  },
  mounted(){
    this.$bus.$on('saoma',(e)=>{
      this.indexTm = e
    })
    this.$bus.$on('CsGzbListInfo',(e)=>{
      this.gzbListInfo = e
      console.log(e,3333)
    })
    // 接收信息给 hcx出库 hcx回库 按钮
    this.$bus.$on('smCsHcxInfo',(e)=>{
      this.hcxBoxCome = []
      this.packageInfo = e;
      console.log("packageInfo",this.packageInfo);
      for(let num=0;num<e.length;num++){
        this.hcxBoxCome.push(e[num].msTaskAoId)
      }
    })

    this.$bus.$on("matchInfo",(e)=>{
      this.matchInfo = e;
    })
  }
};
</script>
<style scoped>
.el-button--success {
  background: #29bacd;
  border-color: #29bacd;
}
.el-button--success:hover {
  background: #23abbd;
  border-color: #23abbd;
}
</style>